package com.devunion.bogdan.view.actions.annotations;

import com.devunion.bogdan.util.IconHelper;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import org.apache.log4j.Logger;

/**
 * @author Viktar Liaskovich
 */
public class AddImageAnnotationAction extends AbstractAddAnnotationAction {
    private Logger log = Logger.getLogger(AddImageAnnotationAction.class);
    private boolean isListenerAdded = false;

    public AddImageAnnotationAction() {
        super(null, IconHelper.getIcon("AddImageAnnotation16.gif"));
        putValue(Action.LARGE_ICON_KEY, IconHelper.getIcon("AddImageAnnotation24.gif"));
    }

    public void actionPerformed(ActionEvent e) {
        final JToggleButton source = (JToggleButton) e.getSource();

        if (!buttonGroup.contains(source)) {
            buttonGroup.add(source);
        }

        if (!isListenerAdded) {
            source.getModel().addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    EditorToolbarManager.getInstance().setAddImageAnnotationToolSelected(source.isSelected());
                }
            });

            isListenerAdded = true;
        }

        EditorToolbarManager.getInstance().setAddImageAnnotationToolSelected(source.isSelected());

        log.debug("Add Image Annotation Tool Selected: " + source.isSelected());
    }
}
